// plugins/toast.js
import { createApp, h } from 'vue'
import ToastMessage from '@/components/modules/ToastMessage/index.vue';

export const vToast = (options) => {
    return new Promise((resolve) => {
        const container = document.createElement('div')
        document.body.appendChild(container)

        const tempApp = createApp({
            render() {
                return h(ToastMessage, {
                    ...(typeof options === 'string' ? { message: options } : options),
                    onClosed: () => {
                        tempApp.unmount()
                        container.remove()
                        resolve() // 自动resolve不需要catch
                    }
                })
            }
        })

        tempApp.mount(container)
    })
}

export default {
    install(app) {
        app.config.globalProperties.$vToast = vToast
        app.component('ToastMessage', ToastMessage);
    }
}